﻿using System.Reflection;
using CommandLine;
using CommandLine.Text;

namespace GenKey
{
    public class Options
    {
        private readonly HeadingInfo _headingInfo;

        private static string exeName()
        {
            return Assembly.GetEntryAssembly().GetName().Name;
        }

        public Options()
        {
            _headingInfo = new HeadingInfo(string.Format("Crypton {0} - crypto key generation utility.", exeName()), 
               "Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString());
        }

        [Option("a", "algorithm", HelpText = "The name of crypto algorithm registered in NET framework."+
            @"Default value is Rijndael. Standard NET algorithms names is Rijndael,RSA,DES,3DES,RC2."+
            @"Third party algorithms can be registered (see GenKey.exe.config file).")]
        public string AlgorithmName = "Rijndael";

        [Option("l", "length", HelpText = "Key length in bits (should be valid for selected crypto algorithm)."+
            @"Default value is maximal key length for selected algorithm."+
            @"For Rijndael algorithm is 128, 192, or 256.For DES algorithm is 64."+
            @"For RC2 algorithm is from 40 bits to 128 bits in increments of 8 bits."+
            @"For TripleDES algorithm is from 128 bits to 192 bits in increments of 64 bits.")]
        public int KeyLength = 0;

        [Option("k", "key", HelpText = "The name of key file.", Required = true)]
        public string Key = "";

        [HelpOption(HelpText = "Dispaly this help screen.")]
        public string GetUsage()
        {
            var help = new HelpText(_headingInfo)
                           {
                               AdditionalNewLineAfterOption = false,
                               Copyright = new CopyrightInfo("Vladimir V.Boborykin", 2010, 2011)
                           };
            help.AddPreOptionsLine(@"".PadRight(70, '-'));
            help.AddPreOptionsLine(string.Format(@"USAGE: {0} [-a[lgorithm] algorithmName] [-l[ength] keyLen] -k[ey] keyName", exeName()));

            help.AddPostOptionsLine(@"EXAMPLES:");
            help.AddPostOptionsLine(@" Generate 256 bits Rijndael key file c:\temp\rijkey.symkey:");
            help.AddPostOptionsLine(string.Format(@"  {0}.exe -key c:\temp\rijkey.symkey", exeName()));
            help.AddPostOptionsLine(@"");
            help.AddPostOptionsLine(@" Generate RSA key files pair c:\temp\newkey.prikey and c:\temp\newkey.pubkey:");
            help.AddPostOptionsLine(string.Format(@"  {0}.exe -a RSA -key c:\temp\newkey", exeName()));

            help.AddOptions(this);

            return help;
        }
    }
}
